#include "stdafx.h"
#include "../GameCore/CGame.h"


void CGame::CreateAnimal(int type)
{
	if(_current_num_animal < ANIMAL_MAX_OBJECT)
	{
		int rand = CMath::RANDOM(0, 1000)%3;
		float x;
		switch(rand)
		{
			case 0:
				x = 0.0;
				break;
			case 1:
				x = k_SCREEN_WIDTH;
				break;
			case 2:
				x = k_SCREEN_WIDTH/2;
				break;
		}
		_animals[_current_num_animal++].Init(_current_num_animal, type, x, 0.0);
	}
}

void CGame::RenderAnimals()
{
	for(int i = 0; i < _current_num_animal; i++)
	{
		if(!_animals[i].IsDie())
			_animals[i].Render();
	}
}

void CGame::UpdateAnimals()
{
	for(int i = 0; i < _current_num_animal; i++)
	{
		if(!_animals[i].IsDie())
			_animals[i].Update();
	}
}

void CGame::ResetAnimals()
{
	for(int i = 0; i < _current_num_animal; i++)
	{
		_animals[i].SetDie();
	}
}

void CGame::CreateItem(float baseX, float baseY)
{
	for(int i = 0; i < ANIMAL_MAX_OBJECT*2; i ++)
	{
		if(_items[i].IsDie())
		{
			int type, rand = CMath::RANDOM(0, 1000)%2;
			switch(rand)
			{
				case 0:
					type = _IMAGE_TYPE_EGG;
					break;
				case 1:
					type = _IMAGE_TYPE_SHIT;
					break;
			}
			_items[i].Init(i, type, baseX, baseY + 100);
			return;
		}
	}
}

void CGame::RenderItems()
{
	for(int i = 0; i < ANIMAL_MAX_OBJECT*2; i++)
	{
		if(!_items[i].IsDie())
			_items[i].Render();
	}
}

void CGame::UpdateItems()
{
	for(int i = 0; i < ANIMAL_MAX_OBJECT*2; i++)
	{
		if(!_items[i].IsDie())
			_items[i].Update();
	}
}

void CGame::ResetItems()
{
	for(int i = 0; i < ANIMAL_MAX_OBJECT*2; i++)
	{
		_items[i].SetDie();
	}
}

void CGame::RenderBasket()
{
	GET_SPRITE().DrawTexture(GL(), _IMAGE_TYPE_BASKET, _basket_posX, _basket_posY);
}

void CGame::UpdateBasket()
{
	//update touch
	UpdateTouchDrag();
	if(_on_key_narrow_left)
	{
		if(_basket_posX > 0)
			_basket_posX -= MOVE_ON_KEY_PRESS_SPEED;
		else
			_on_key_narrow_left = false;
	}
	else if(_on_key_narrow_right)
	{
		if(_basket_posX < k_SCREEN_WIDTH - 50)
			_basket_posX += MOVE_ON_KEY_PRESS_SPEED;
		else
			_on_key_narrow_right = false;
	}
}

void CGame::UpdateTouchDrag()
{
	bool flag = false;
	for(int i = 0; i < GET_GAME()->_num_touches;i++)
	{
		if(!GET_TOUCH(i)->_flag)
		{
			continue;
		}
		if(GET_TOUCH(i)->_state == TOUCH_UP)
		{
			GET_GAME()->ClearAllTouch();
			break;
		}
		else if(GET_TOUCH(i)->_state == TOUCH_DRAG)
		{
			int x = GET_TOUCH(i)->x;
			if(x > k_SCREEN_WIDTH - 50)
				x = k_SCREEN_WIDTH - 50;
			if(x < 50)
				x = 50;
			_basket_posX = x - 50;
			break;
		}
	}
}

bool CGame::CheckCircleAndRect(float Cx, float Cy, float Cr, float Rx, float Ry, float Rw, float Rh)
{
	float distanceX = CMath::ABSOLUTE_VALUE(Cx - (Rx+Rw/2)); 
    float distanceY = CMath::ABSOLUTE_VALUE(Cy - (Ry+Rh/2));

    if (distanceX > (Rw/2 + Cr)) { return false; }
    if (distanceY > (Rh/2 + Cr)) { return false; }

    if (distanceX <= (Rw/2)) { return true; } 
    if (distanceY <= (Rh/2)) { return true; }

	float cornerDistance_sq = (distanceX - Rw/2)*(distanceX - Rw/2) + (distanceY - Rh/2)*(distanceY - Rh/2);
	return (cornerDistance_sq <= Cr*Cr);
}

void CGame::RemoveCharaterHP()
{
	GET_CONFIG()._current_charater_hp --;
	printf("GET_CONFIG()._current_charater_hp = %d", GET_CONFIG()._current_charater_hp);
	if(GET_CONFIG()._current_charater_hp <= 0)
		SetGameState(k_GS_RESULT,true);
}
void CGame::AddEggs()
{
	GET_CONFIG()._current_egg_count ++;
	GET_CONFIG()._current_level = GET_CONFIG()._current_egg_count/20;
}

int CGame::GetCurrentLevel()
{
	return GET_CONFIG()._current_level;
}
